package com.union.core.sys.util;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.hnjowe.webdb.DBOperation;
import com.union.core.sys.dao.impl.SysLogsDaoImpl;
import com.union.core.sys.model.SysLogs;
import com.union.core.sys.model.SysStatic;

public class BaseUtil {

	private DBOperation opera;
	private SysLogs sysLogs;
	private static Logger log = Logger
			.getLogger(SysLogsDaoImpl.class.getName());

	public BaseUtil() {
	}

	public static List<SysStatic> getStaticList(String type_id,
			String subSysCode, int validFlag) {

		List<SysStatic> sysStaticList = new ArrayList<SysStatic>();
		DBOperation opera = new DBOperation();
		try {
			opera.transcationBegin();

			String sql = "select "
					+ " DATA_ID,DATA_NAME "
					+ " from TD_S_STATIC"
					+ " where type_id = ? and subsys_code = ? and valid_flag = ?"
					+ " order by DATA_ID asc";
			opera.prepStatment(sql);
			opera.setString(1, type_id);
			opera.setString(2, subSysCode);
			opera.setInt(3, validFlag);
			ResultSet res = opera.executeQuery();
			int i = 0;
			while (res.next()) {
				SysStatic sysStatic = new SysStatic();
				sysStatic.setDATA_ID(res.getInt(1));
				sysStatic.setDATA_NAME(res.getString(2));
				sysStaticList.add(i, sysStatic);
				i++;
			}
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return sysStaticList;
	}

	public static SysStatic getStatic(String type_id, String subSysCode,
			int dataId, int validFlag) {
		SysStatic sysStatic = new SysStatic();
		DBOperation opera = new DBOperation();
		try {
			opera.transcationBegin();

			String sql = "select "
					+ " DATA_ID,DATA_NAME "
					+ " from TD_S_STATIC"
					+ " where type_id = ? and subsys_code = ? and data_id = ? and valid_flag = ?"
					+ " order by DATA_ID desc";
			opera.prepStatment(sql);
			opera.setString(1, type_id);
			opera.setString(2, subSysCode);
			opera.setInt(3, dataId);
			opera.setInt(4, validFlag);
			ResultSet res = opera.executeQuery();
			if (res.next()) {
				sysStatic.setDATA_ID(res.getInt(1));
				sysStatic.setDATA_NAME(res.getString(2));
			}
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return sysStatic;
	}
	
	public static String getOrganizationNameById(int organizationId) {
		String organizationName = null;
		DBOperation opera = new DBOperation();
		try {
			opera.transcationBegin();
			String sql = "select "
					+ " organization_name "
					+ " from organization"
					+ " where organization_id = ? ";
			opera.prepStatment(sql);
			opera.setInt(1, organizationId);
			log.info(opera.getPres().toString());
			ResultSet res = opera.executeQuery();
			if (res.next()) {
				organizationName = res.getString(1);
			}
		} catch (Exception e) {
			log.error(e);
			log.error(opera.getPres().toString());
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return organizationName;
	}
	
	
	public static String getStaffNameNameById(int staffId) {
		String staffName = null;
		DBOperation opera = new DBOperation();
		try {
			opera.transcationBegin();
			String sql = "select "
					+ " personnel_name "
					+ " from personnel"
					+ " where personnel_id = ? ";
			opera.prepStatment(sql);
			opera.setInt(1, staffId);
			log.info(opera.getPres().toString());
			ResultSet res = opera.executeQuery();
			if (res.next()) {
				staffName = res.getString(1);
			}
		} catch (Exception e) {
			log.error(e);
			log.error(opera.getPres().toString());
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return staffName;
	}
	
	

	public static String getStatucValue(String type_id, String subSysCode,
			int dataId, int validFlag) {
		{
			return getStatic(type_id, subSysCode, dataId, validFlag)
					.getDATA_NAME();
		}

	}
	
	public static String getDateFormat(String str,String format){
	   SimpleDateFormat sdf=new SimpleDateFormat(format);
	   String datestr = null;
	   try{
		   datestr=sdf.format(sdf.parse(str));
	   }catch(Exception e){
		   e.printStackTrace();
		   log.error(e.toString());
	  }
	  return datestr;
	}

}
